package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetTaskRecordTable(ctx *context.Context) table.Table {

	taskRecord := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := taskRecord.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("姓名", "name", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("种子品种", "variant", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("发货数量", "quantity", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("额外标签", "labels", db.Longtext).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("司机电话", "driver_phone", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("司机姓名", "driver_name", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("任务日期", "task_date", db.Date).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("任务状态", "status", db.Tinyint).
		FieldDisplay(func(model types.FieldModel) interface{} {
			switch model.Value {
			case "0":
				return "未知"
			case "1":
				return "已发布"
			case "2":
				return "已执行"
			default: // 未知
				return "未知"
			}
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Text: "未知", Value: "0"},
			{Text: "已发布", Value: "1"},
			{Text: "已执行", Value: "2"},
		}).
		FieldEditAble(editType.Select).FieldEditOptions(types.FieldOptions{
		{Text: "未知", Value: "0"},
		{Text: "已发布", Value: "1"},
		{Text: "已执行", Value: "2"},
	}).
		FieldSortable()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("oss object地址", "oss_url", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("oss 完整访问地址", "access_url", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("更新时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("task_record").SetTitle("任务记录").SetDescription("TaskRecord")

	formList := taskRecord.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate()
	formList.AddField("姓名", "name", db.Varchar, form.Text)
	formList.AddField("种子品种", "variant", db.Varchar, form.Text)
	formList.AddField("发货数量", "quantity", db.Varchar, form.Text)
	formList.AddField("额外标签: [{\"key\":\"品种1\",\"value\":\"20\"},{\"key\":\"品种2\",\"value\":\"30\"},{\"key\":\"品种3\",\"value\":\"60\"}]", "labels", db.Longtext, form.RichText)
	formList.AddField("司机电话", "driver_phone", db.Varchar, form.Text)
	formList.AddField("司机姓名", "driver_name", db.Varchar, form.Text)
	formList.AddField("任务日期", "task_date", db.Date, form.Datetime)
	formList.AddField("任务状态", "status", db.Tinyint, form.Radio).
		// radio的选项，Text代表显示内容，Value代表对应值
		FieldOptions(types.FieldOptions{
			{Text: "未知", Value: "0"},
			{Text: "已发布", Value: "1"},
			{Text: "已执行", Value: "2"},
		}).FieldDefault("0")
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("oss object 地址", "oss_url", db.Varchar, form.Text)
	formList.AddField("oss 完整访问地址", "access_url", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("task_record").SetTitle("任务记录").SetDescription("TaskRecord")

	return taskRecord
}
